package br.com.quiz.dao;

import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;
import org.hibernate.Query;
import org.hibernate.Session;

import br.com.quiz.domain.Aluno;
import br.com.quiz.domain.Professor;
import br.com.quiz.util.HibernateUtil;

public abstract class ProfessorDAO{
	
	
	public static void salvarOuEditarCriptografado(Professor p){
		p.setPesSenha(DigestUtils.md2Hex( p.getPesSenha() ) );
		AbstractDAO.salvarOuEditar( p );
	}
	
	public static List< Professor > listar() {
		Session sessao = HibernateUtil.getSessionFactory().openSession();
		List< Professor > professor = null;
		try {
			Query consulta = sessao.getNamedQuery( "Professor.listar" );
			professor = consulta.list();
		} catch ( RuntimeException e ) {
			throw e;
		} finally {
			sessao.close();
		}

		return professor;
	}

	public static Professor filtrarPorId( int proId ) {
		Session sessao = HibernateUtil.getSessionFactory().openSession();
		Professor professor = null;
		try {
			Query consulta = sessao.getNamedQuery( "Professor.filtrarPorId" );
			consulta.setInteger( "proId", proId );
			professor = (Professor) consulta.uniqueResult();
		} catch ( RuntimeException e ) {
			throw e;
		} finally {
			sessao.close();
		}

		return professor;
	}

	public static Long verificarProfessor( int pesId ) {
		Session sessao = HibernateUtil.getSessionFactory().openSession();

		try {
			String hql = "select count(p.pesId) from Pessoa p where p.pesId in (select pro.pesId from Professor pro where pro.pesId = p.pesId) and p.pesId= " + pesId;
			Query query = sessao.createQuery( hql );
			Long quantidadesProfessoresEncontrados = (Long) query.uniqueResult();

			
				return quantidadesProfessoresEncontrados;

			
		} catch ( RuntimeException ex ) {
			throw ex;
		} finally {
			sessao.close();
		}

	}

}
